package com.goodjia.phase1module4;

public class Worker {

    private String name;
    private int age;

    public Worker() {
    }

    public Worker(String name, int age) {
        setName(name);
        setAge(age);
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        // 当年龄为null或者空字符串时抛出自定义的空异常
        if (name == null || name.equals("")) {
            try {
                throw new NullException("姓名不能为空哟！");
            } catch (NullException e) {
                e.printStackTrace();
            }
        } else {
            this.name = name;
        }
    }

    public void setAge(int age) {
        // 当年龄小于18大于150时抛出自定义的年龄异常
        if (age >= 18 && age <= 150) {
            this.age = age;
        } else {
            try {
                throw new AgeException("年龄不合理");
            } catch (AgeException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return "Worker{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
    }

}
